// src/store/Modules.ts

import type {GetUserInfoReqVO, UserInfo} from "@/interfaces/user-info";
import {useGetUserInfoApi} from "@/services/api";

export default {
    auth: {
        namespaced: true,
        state: {
            userInfo: {
                nickName: "",
                phoneNumber: "",
                avatar: "",
                wallet: 0.00,
                orderCount:0
            },
            userId: "",
            token: "",
            activeTab: "index"
        },
        mutations: {
            setToken(_state: any, token: string) {
                if (token) {
                    _state.token = token
                } else {
                    _state.token = ""
                }
            },
            setUserInfo(_state: any, userInfo: UserInfo) {
                if (userInfo) {
                    _state.userInfo = userInfo
                } else {
                    _state.userInfo = {
                        nickName: "",
                        phoneNumber: "",
                        avatar: "",
                        wallet: 0.00,
                        orderCount:0
                    }
                }
            },
            setUserId(_state: any, userId: string) {
                if (userId) {
                    _state.userId = userId
                } else {
                    _state.userId = ""
                }
            },
            setActiveTab(_state: any, activeTab: string) {
                if (activeTab) {
                    _state.activeTab = activeTab
                } else {
                    _state.activeTab = ""
                }
            },
        },
        actions: {
            setToken(context: any, token: string) {
                context.commit('setToken', token)
            },
            setUserInfo(context: any, userInfo: UserInfo) {
                context.commit('setUserInfo', userInfo)
            },
            setUserId(context: any, userId: string) {
                context.commit('setUserId', userId)
            },
            setActiveTab(context: any, activeTab: string) {
                context.commit('setActiveTab', activeTab)
            },
            logout(context: any) {
                context.commit('setToken')
                context.commit('setUserInfo')
                context.commit('setUserId')
                uni.navigateTo({
                    url: '/pages/login/index',
                    success: () => {
                        uni.showToast({
                            title: '你已被登出，请重新登录',
                            icon: 'none'
                        })
                    }
                })
            },
            reloadUserInfo(context: any) {
                const data: GetUserInfoReqVO = {id: context.getters.getUserId}
                useGetUserInfoApi(data)
                    .then(res => {
                        context.commit('setUserInfo',res.data)
                    })
            }
        },
        getters: {
            getToken(_state: any, _getters: any): string {
                return _state.token
            },
            getUserInfo(_state: any, _getters: any): string {
                return _state.userInfo
            },
            getUserId(_state: any, _getters: any): string {
                return _state.userId
            },
            getActiveTab(_state: any, _getters: any): string {
                return _state.activeTab
            }
        }
    }
}